<?php

$server->wsdl->addComplexType(
    'I07',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'I07_BAR_KODAS' => array('name'=>'I07_BAR_KODAS','type'=>'xsd:string'),
		'I07_KODAS' => array('name'=>'I07_KODAS','type'=>'xsd:string'),
		 'I07_PAV' => array('name'=>'I07_PAV','type'=>'xsd:string'),
		 'I07_KIEKIS' => array('name'=>'I07_KIEKIS','type'=>'xsd:decimal'), 
		  'I07_FRAKCJA' => array('name'=>'I07_FRAKCJA','type'=>'xsd:decimal'),
		 'I07_KODAS_US_P' => array('name'=>'I07_KODAS_US_P','type'=>'xsd:string'),
		 'I07_KAINA_BE' => array('name'=>'I07_KAINA_BE','type'=>'xsd:decimal'), 
		  'I07_MOKESTIS_P' => array('name'=>'I07_MOKESTIS_P','type'=>'xsd:decimal'),
		  'I07_TIPAS' => array('name'=>'I07_TIPAS','type'=>'xsd:decimal')
		  
    )
);

$server->wsdl->addComplexType(
    'I06',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'I06_DOK_NR' => array('name'=>'I06_DOK_NR','type'=>'xsd:string'),
        'I06_OP_DATA' => array('name'=>'I06_OP_DATA','type'=>'xsd:date'),
		'I06_KODAS_KS' => array('name'=>'I06_KODAS_KS','type'=>'xsd:string'),
		'I06_PAV' => array('name'=>'I06_PAV','type'=>'xsd:string'), 
		'I06_SUMA' => array('name'=>'I06_SUMA','type'=>'xsd:decimal'),
		'I06_SUMA_PVM' => array('name'=>'I06_SUMA_PVM','type'=>'xsd:decimal'),
		'I06_SUMA_VISO' => array('name'=>'I06_SUMA_VISO','type'=>'xsd:decimal'), 
		'I06_PASTABOS' => array('name'=>'I06_PASTABOS','type'=>'xsd:string'),
		'I06_ADR' => array('name'=>'I06_ADR','type'=>'xsd:string'),
		'I06_KODAS_AK' => array('name'=>'I06_KODAS_AK','type'=>'xsd:string'),
		'I06_PAV2' => array('name'=>'I06_PAV2','type'=>'xsd:string'),
		'I06_KODAS_VS' => array('name'=>'I06_KODAS_VS','type'=>'xsd:string'),
		'I06_ADR2' => array('name'=>'I06_ADR2','type'=>'xsd:string'),
		'I07' => array('name'=>'I07','type'=>'tns:I07Array')		
    )
);


$server->wsdl->addComplexType(
	'I06Array',
	'complexType',
	'array',
	'',
	'SOAP-ENC:Array',
	array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:I06[]')
    ),
    'tns:I06'
);
$server->wsdl->addComplexType(
	'I07Array',
	'complexType',
	'array',
	'',
	'SOAP-ENC:Array',
	array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:I07[]')
    ),
    'tns:I07'
);

  $server->register('xml_sask',
			array('VART_ID' => 'xsd:string','PASS' => 'xsd:string','StartDate' => 'xsd:date','EndDate' => 'xsd:date'),  //parameters			
			array('return' => 'tns:I06Array'),  //output
			'urn:server',   //namespace
			'urn:server#xml_saskServer',  //soapaction
			'rpc', // style
			'encoded', // use
			'<br/>I06_DOK_NR - S&#261;skaitos serija numeris
<br/>I06_OP_DATA - Data
<br/>I06_KODAS_KS - Kliento kodas
<br/>I06_PAV - Pavadinimas
<br/>I06_ADR - Registracijos adresas.
<br/>I06_KODAS_AK - Pristatymo adreso kodas
<br/>I06_PAV2 - Pristatymo adreso pavadinimas
<br/>I06_KODAS_VS - Vietov&#279;s kodas
<br/>I06_ADR2 - Pristatymo adresas
<br/>I06_SUMA - suma be PVM
<br/>I06_SUMA_PVM - PVM suma
<br/>I06_SUMA_VISO - Suma viso
<br/>I06_PASTABOS - Pastabos
<br/><br/>DI07_BAR_KODAS - Prek&#279;s barkodas
<br/>I07_KODAS - Prek&#279;s kodas
<br/>I07_PAV - Prek&#279;s pavadinimas
<br/>I07_KIEKIS - Kiekis
<br/>I07_FRAKCJA - Frakcija
<br/>I07_KODAS_US_P - Mato vnt
<br/>I07_KAINA_BE - Kaina be PVM
<br/>I07_MOKESTIS_P - PVM tarifas
<br/>I07_TIPAS - ');  //description
 
function xml_sask($vart_id,$pass,$start,$end) {
    global $db_www,$db_rdata,$i_query,$fakt_query,$accounts_query2;

	$rez = checkConnection(); if ($rez != "") return $rez;
	
	$uid = checkUserRights($vart_id,$pass); 
	if ($uid == "") {
	  return new soap_fault('-3', '', 'Bad username or password!','');
	}
	if(!validateDate($start, 'Y-m-d')){
	  return new soap_fault('-4', '', 'Bad start date!','');
	}
	if(!validateDate($end, 'Y-m-d')){
	  return new soap_fault('-4', '', 'Bad end date!','');
	}	
	$i_query = str_replace("<kodas>", $uid, $i_query);
	$minfo = $db_www->returnInfo($i_query);	
	
	
	$accounts_query2 =  "SELECT I06_KODAS_PO,I06_OP_TIP,I06_DOK_NR,CONVERT(VARCHAR(10), I06_OP_DATA, 102) AS I06_OP_DATA,i06_kodas_ks,I06_KODAS_KS+' '+I06_PAV as i06_pav,I06_PASTABOS,
						I06_SUMA+I06_SUMA_PVM as i06_suma,i06_perkelta,i06_kodas_ls_4,I06_KODAS_MS,I06_PAV,I06_SUMA,I06_SUMA_PVM,
						I06_ADR,I06_KODAS_AK,I06_PAV2,I06_KODAS_VS,I06_ADR2
						FROM I06_PARH
						where (I06_OP_TIP=51 or I06_OP_TIP=52) and (I06_KODAS_KS = '".trim($minfo["N08_kodas_ks"])."')";
	$accounts_query2 .= " AND I06_OP_DATA BETWEEN CONVERT(datetime,'".$start."',102) AND CONVERT(datetime,'".$end."',102)";
		
	$accounts2 = $db_www->returnList($accounts_query2 );	
	
	$result = array();
	foreach ($accounts2 as $row) {
		$items = $db_www->returnList("select i.*,case when n.N37_BAR_KODAS is null then '' else n.N37_BAR_KODAS end as N37_BAR_KODAS from I07_PARD i LEFT OUTER JOIN n37_pmat n on i.I07_KODAS=n.N37_KODAS_PS where i.I07_KODAS_PO='".addslashes_mssql($row["I06_KODAS_PO"])."'");
			
		if (!empty($items)) {
			$res = array();
			foreach ($items as $item) {
				$res[] = array(
				'I07_BAR_KODAS'=>@iconv('windows-1257', 'UTF-8',$item['N37_BAR_KODAS']),
				'I07_KODAS'=>@iconv('windows-1257', 'UTF-8',$item['I07_KODAS']),
				'I07_PAV'=>@iconv('windows-1257', 'UTF-8',$item['I07_PAV']),
				'I07_KIEKIS'=>@iconv('windows-1257', 'UTF-8',$item['I07_KIEKIS']),
				'I07_FRAKCJA'=>@iconv('windows-1257', 'UTF-8',$item['I07_FRAKCJA']),
				'I07_KODAS_US_P'=>@iconv('windows-1257', 'UTF-8',$item['I07_KODAS_US_P']),
				'I07_KAINA_BE'=>@iconv('windows-1257', 'UTF-8',$item['I07_KAINA_BE']),
				'I07_MOKESTIS_P'=>@iconv('windows-1257', 'UTF-8',$item['I07_MOKESTIS_P']),
				'I07_TIPAS'=>@iconv('windows-1257', 'UTF-8',$item['I07_TIPAS'])
				);
			}

			$result[] = array(		
			'I06_DOK_NR'=>@iconv('windows-1257', 'UTF-8',$row["I06_DOK_NR"]),
			'I06_OP_DATA'=>@iconv('windows-1257', 'UTF-8',$row["I06_OP_DATA"]),
			'I06_KODAS_KS'=>@iconv('windows-1257', 'UTF-8',$row["i06_kodas_ks"]),
			'I06_PAV'=>@iconv('windows-1257', 'UTF-8',$row["I06_PAV"]),
			'I06_SUMA'=>@iconv('windows-1257', 'UTF-8',$row["I06_SUMA"]),
			'I06_SUMA_PVM'=>@iconv('windows-1257', 'UTF-8',$row["I06_SUMA_PVM"]),
			'I06_SUMA_VISO'=>@iconv('windows-1257', 'UTF-8',$row["i06_suma"]),
			'I06_PASTABOS'=>@iconv('windows-1257', 'UTF-8',$row["I06_PASTABOS"]),
			'I06_ADR'=>@iconv('windows-1257', 'UTF-8',$row["I06_ADR"]),
			'I06_KODAS_AK'=>@iconv('windows-1257', 'UTF-8',$row["I06_KODAS_AK"]),
			'I06_PAV2'=>@iconv('windows-1257', 'UTF-8',$row["I06_PAV2"]),
			'I06_KODAS_VS'=>@iconv('windows-1257', 'UTF-8',$row["I06_KODAS_VS"]),
			'I06_ADR2'=>@iconv('windows-1257', 'UTF-8',$row["I06_ADR2"]),			
			'I07'=>$res
			);	
		}
	}
	return $result;

}


?>